import * as api from '../../services/example';
import { Toast } from 'antd-mobile';

export default {
  namespace: 'home',
  state: {
    down: false,
    height: document.documentElement.clientHeight,
    data: [],
    loading: false,
  },
  reducers: {
    changeHeight(state, { payload }) {
      return {
        ...state,
        height: payload.height,
      };
    },
    querySuccess(state, { payload }) {
      const { data } = payload;
      return {
        ...state,
        data,
        loading: false,
      };
    },
    loading(state, { payload }) {
      const { flag } = payload;
      return {
        ...state,
        loading: flag,
      };
    },
  },
  effects: {
    *query({ payload = {} }, { call, put, select }) {
      yield put({ type: 'loading', payload: { flag: true } });
      const ticket = sessionStorage.getItem('ticket');
      const { data } = yield call(api.initTicket, {
        ticket: sessionStorage.getItem('ticket'),
        ip: sessionStorage.getItem('ClientIp'),
      });
      const { result, msg } = data;
      if (result === 'false') {
        Toast.fail(msg);
        yield put({ type: 'loading', payload: { flag: false } });
      } else {
        const { data } = yield call(api.validatePerInfo, {
          id: sessionStorage.getItem('personId'),
        });
        sessionStorage.setItem('usercode',data.usercode)
        const result = yield call(api.queryDyList, data);
        yield put({ type: 'querySuccess', payload: { data: result.data.respDataAll } });
      }
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        // dispatch({ type: 'query' });
        const { pathname } = location;
        if (pathname === '/daiyue') {//包名
          let timer = setInterval(() => {
            const ticket = sessionStorage.getItem('ticket');
            if (ticket) {
              clearInterval(timer);
              dispatch({ type: 'query' });
            } else {
              dispatch({ type: 'loading', payload: {flag: true}});
            }
          }, 1000);
        }
      });
    },
  },
};
